package Thread.letter;

import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

public class Test2 {
    @Test
    void test() {
        Object lock = new Object();
        new Thread(new Test2Thread1(lock)).start();
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(new Test2Thread2(lock)).start();
    }
}

class Test2Thread1 implements Runnable {
    private Object lock;

    public Test2Thread1(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        synchronized (lock) {
            System.out.println("开始wait：" + System.currentTimeMillis());
            try {
                lock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("结束wait：" + System.currentTimeMillis());
            lock.notify();
        }
    }
}

class Test2Thread2 implements Runnable {
    private Object lock;

    public Test2Thread2(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        synchronized (lock) {
            System.out.println("开始notify：" + System.currentTimeMillis());
            lock.notify();
            try {
                lock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("结束notify：" + System.currentTimeMillis());
            lock.notify();
        }
    }
}
